package com.example.its2021606134chh.TrafficQueryModule;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.its2021606134chh.R;

/**
 * @ClassName: TQShow$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 3/9/2021$
 * @Version: 1.0
 */
public class TQREShow extends View {
    private int color = R.color.white;
    public void setState(int state) {
        switch (state) {
            case 1:
                this.color = R.color.colorOpen;
                break;
            case 2:
                this.color = R.color.colorSlow;
                break;
            case 3:
                this.color = R.color.colorGeneralCongestion;
                break;
            case 4:
                this.color = R.color.colorModerateCongestion;
                break;
            case 5:
                this.color = R.color.colorSevereCongestion;
                break;
        }
    }
    public TQREShow(Context context) {
        super(context);
    }

    public TQREShow(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int r = getMeasuredWidth() / 2;
        int stroke=30;
        int left = getLeft();
        int top = getTop();
        int right = getRight();
        int bottom = getBottom();
        int centerX = getLeft() + r;
        int centerY = getTop() + r;
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setAntiAlias(true);
        paint.setColor(getResources().getColor(color, null));
        paint.setStyle(Paint.Style.STROKE);//空心
        paint.setStrokeWidth(stroke);//空心圆的边框
        RectF oval3 = new RectF(left+(3*stroke), top+(3*stroke), right-(3*stroke), bottom-(3*stroke));// 设置个新的长方形
//        canvas.drawRect(left+(2*stroke),top+(2*stroke),right-(2*stroke),bottom-(2*stroke),paint);
        canvas.drawRoundRect(oval3, 20, 15, paint);
//        Paint paint1 = new Paint();
//        String text = "环城快速路" ;
//        Paint.FontMetrics fontMetrics = paint1.getFontMetrics();
//        float baseline = (oval3.bottom + oval3.top - fontMetrics.bottom - fontMetrics.top) / 2;
//        canvas.drawText(text, oval3.centerX(), baseline, paint1);
//        Paint p = new Paint();
//        p.setColor(Color.RED);// 设置红色
        //画圆角矩形
//        p.setStyle(Paint.Style.FILL);//充满
//        p.setColor(Color.LTGRAY);
//        p.setAntiAlias(true);// 设置画笔的锯齿效果
//        canvas.drawText("画圆角矩形:", 10, 260, p);
//        RectF oval3 = new RectF(80, 260, 200, 300);// 设置个新的长方形
//        canvas.drawRoundRect(oval3, 20, 15, p);//第二个参数是x半径，第三个参数是y半径
//        canvas.drawCircle(centerX+(stroke/2), centerY+(stroke/2), r, paint);
    }

    @Override
    public void invalidate() {
        super.invalidate();
    }
}
